//
//  ViewController.m
//  UI11_homework_Movielist
//
//  Created by aaaaaa on 16/6/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#import "Movie.h"
#import "CustomTableViewCell.h"

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray<Movie *> *movieArray;
@property (nonatomic, retain) UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self config];
    [self handleData];
    [self createTableView];
    
}


- (void)config{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"电影";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.rightBarButtonItem = [self editButtonItem];
}

- (void)handleData{
    
    self.movieArray = [NSMutableArray array];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"movielist" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSError *error = nil;
    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
    NSArray *array = [dictionary objectForKey:@"result"];
    
    for (NSDictionary *dic in array) {
        Movie *movie = [[Movie alloc] init];
        [movie setValuesForKeysWithDictionary:dic];
        [self.movieArray addObject:movie];
        [movie release];
    }
}

- (void)createTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - 64)];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:_tableView];
    [_tableView release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.movieArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    Movie *movie = [self.movieArray objectAtIndex:indexPath.row];
    
    cell.movie = movie;
    
    
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 180;
}


//编辑
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
            return UITableViewCellEditingStyleInsert;
            break;
            
        default:
            break;
    }
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (editingStyle) {
        case UITableViewCellEditingStyleInsert:
        {
            Movie *movie = [[Movie alloc] init];
            movie.movieName = @"add";
            movie.movieId = @"146554";
            [self.movieArray insertObject:movie atIndex:indexPath.row];
            [movie release];
        }
            break;
        case UITableViewCellEditingStyleDelete:
        {
            [self.movieArray removeObjectAtIndex:indexPath.row];
        }
        default:
            break;
    }
    
    [tableView reloadData];
}

//移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
    Movie *movie = [self.movieArray objectAtIndex:sourceIndexPath.row];
    
    [movie retain];
    [self.movieArray removeObjectAtIndex:sourceIndexPath.row];
    
    [self.movieArray insertObject:movie atIndex:destinationIndexPath.row];
    [movie release];
    
    
}





















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
